//
//  ViewController.m
//  囧途
//
//  Created by fly on 16/3/3.
//  Copyright © 2016年 fly. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

- (void)initializeAppearance;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - privete methods
- (void)initializeAppearance{

    self.selectedIndex = 0;
}

#pragma mark - setter
- (void)setSelectedIndex:(NSInteger)selectedIndex{
    UIViewController *currentViewController = self.viewContorllers[_selectedIndex];
    // 1 、移除视图
    [currentViewController.view removeFromSuperview];
    // 2、移除控制器
    [currentViewController removeFromParentViewController];
    // 3、调用子控制器的回调
    [currentViewController willMoveToParentViewController:nil];
    
    _selectedIndex = selectedIndex;
    
    // 添加
    UIViewController *selectedViewController = self.viewContorllers[_selectedIndex];
    // 1、关联控制器
    [self addChildViewController:selectedViewController];
    // 2、关联视图
        [self.view addSubview:selectedViewController.view];
    
    [self.view insertSubview:selectedViewController.view atIndex:0];
    // 3、调用子控制器的回调方法
    [selectedViewController didMoveToParentViewController:self];
    
    
}
- (void)setViewContorllers:(NSArray *)viewContorllers{
    _viewContorllers = viewContorllers;
    [self initializeAppearance];
}


@end

